package com.github.llyb120.performance;

import org.beetl.sql.core.*;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;

/**
 * @Author: Administrator
 * @Date: 9/13/2021 3:20 PM
 */
@Profile("kun")
@Configuration
public class BeetlSqlConfig {

    @Autowired
    DataSource datasource;

    @Bean
    SQLManager sqlManager(){
        ConnectionSource source = ConnectionSourceHelper.getSingle(datasource);
//source是唯一必须的参数，其他参数都有默认值
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
//设置NameConversion，这里数据库命名采用下划线风格，使用UnderlinedNameConversion
        builder.setNc(new DefaultNameConversion());
//设置一个拦截器，输出debug日志，包含了sql语句和执行参数，执行时间
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
//设置数据库分隔，必须跟数据库一样
        builder.setDbStyle(new MySqlStyle());
        SQLManager sqlManager = builder.build();
        return sqlManager;
    }
}
